package com.aerospace.flink;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * @ClassName KafkaConsumerApp
 * @Description
 * @Author yuhang11
 * @Date 2022/10/12 11:33 上午
 * @Version 1.0
 */
public class KafkaConsumerApp {
    public static void main(String[] args){
        try {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            Properties properties = new Properties();
            properties.setProperty("bootstrap.servers", "localhost:9092");
            properties.setProperty("group.id", "flink");
            DataStream<String> stream = env.addSource(new FlinkKafkaConsumer<String>(
                    "test", new SimpleStringSchema(), properties) );
            stream.map(new MapFunction<String, String>() {
                private static final long serialVersionUID = -6867736771747690202L;
                @Override
                public String map(String value) throws Exception {
                    return "flink : " + value;
                }
            }).print();
            env.execute("consumer");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
